/*-------------------------------------------------------------------------
 - Copyright (c) 2024-2025 [XD-AMCC TEAM]
 - [XD-AMCC] is licensed under Mulan PSL v2.
 - You can use this software according to the terms and conditions of the Mulan PSL v2.
 - You may obtain a copy of Mulan PSL v2 at:
 -          http://license.coscl.org.cn/MulanPSL2
 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 - OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 - TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 - See the Mulan PSL v2 for more details.
 ------------------------------------------------------------------------*/

/**
 * @brief Implement the manager for temporary variables and labels.
 *
 * @author  WXQ#XDU
 * @date    2025.01
 */

#include <stdio.h>
#include <stdlib.h>

#include "_emit_inner.h"

t_tempmgr_ptr tempmgr_new(t_emitter_ptr emitterPtr) {
    t_tempmgr_ptr
    pThis = (t_tempmgr_ptr)calloc(1, sizeof(*pThis));
    if (NULL == pThis)
        return NULL;

    pThis->functionStack = &emitterPtr->functionStack;
    return pThis;
}


void tempmgr_destroy(t_tempmgr_ptr  pThis) {
    if (NULL == pThis) return;
    free ( pThis );
}


t_tempvar_id tempmgr_newTemp(t_tempmgr_ptr  pThis) {
    t_emitter_funcinfo *
    funcinfo = (t_emitter_funcinfo*)stack_top(pThis->functionStack);

    ++ pThis->varCounter ;
    return funcinfo->nextTempVarNumber += 1;
}

